﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using DeploymentCopy.Helper;
using DeploymentCopy.Model;
using DeploymentTool.Model;
using DeploymentTool.ViewModel;
using Ionic.Zip;
using NUnit.Framework;
using Newtonsoft.Json;
using DataService = DeploymentTool.Model.DataService;

namespace DeploymentTool.Test
{
    [TestFixture]
    public class DeploymentToolTest
    {
        [Test]
        public void TestMethod()
        {
            MainViewModel vm = new MainViewModel(new DataService());
            vm.LoadConfig.Execute(null);
            vm.SaveConfig.Execute(null);
            FileInfo fi = new FileInfo("Config.json");
            Assert.IsTrue(fi.Exists);
            
        }

        [Test]
        public void TestGenerateConfig()
        {
            MainViewModel vm = new MainViewModel(new DataService());
            vm.Config = new DepConfig()
                            {
                                SearchPatterns = new List<DepSearchPattern>
                                                     {
                                                         new DepSearchPattern(){Enable = true, UseRegularExpression = true, SearchPattern = "123"},
                                                         new DepSearchPattern(){Enable = true, UseRegularExpression = true, SearchPattern = "456"},
                                                         new DepSearchPattern(){Enable = true, UseRegularExpression = true, SearchPattern = "789"},
                                                     },
                                ReplacePatterns = new List<DepReplacePattern>
                                                      {
                                                          new DepReplacePattern(){ Enable = true, UseRegularExpression = true, SearchPattern = "123", ReplacePattern = "qwe"},
                                                          new DepReplacePattern(){ Enable = true, UseRegularExpression = true, SearchPattern = "456", ReplacePattern = "asd"},
                                                          new DepReplacePattern(){ Enable = true, UseRegularExpression = true, SearchPattern = "789", ReplacePattern = "zxc"}
                                                      },
                                IgnoredExtensions = new List<string>{".cs"}
                            };
            vm.SaveConfig.Execute(null);
        }

        [Test]
        public void TestRegex()
        {
            MainViewModel vm = new MainViewModel(new DataService());
            vm.LoadConfig.Execute(null);
        }

        [Test]
        public void TestSerializeDictionary()
        {
            Config config = new Config()
                                {
                                    DefaultDeployMode = EnumDeployMode.Staging,
                                    LookupDirectory = "ZipFiles\\",
                                    RegularSearchPattern = @"201[345]-[01]\d-[0123]\d\s[012]\d-[012345]\d",
                                    CulturalName = CultureInfo.CurrentCulture.Name,
                                    DateFormat = "yyyy-MM-dd HH-mm",
                                    StartDateFilter = "2013-04-02 00-00"
                                };
            config.FolderPaths = new Dictionary<string, FolderPath>();
            config.FolderPaths.Add("MSCU".ToLower(), new FolderPath()
                                                         {
                                                             Staging = @"C:\inetpub\wwwroot\SalesSpecialist\MSCU",
                                                             Product = @"C:\Domains\MicrosoftSalesSpecialist.com\MSCU"
                                                         });
            config.FolderPaths.Add("Sales-Excellence".ToLower(), new FolderPath()
            {
                Staging = @"C:\inetpub\wwwroot\SalesSpecialist\wwwroot",
                Product = @"C:\Domains\MicrosoftSalesSpecialist.com\wwwroot"
            });
            config.FolderPaths.Add("Pre-Sales-Technical".ToLower(), new FolderPath()
            {
                Staging = @"C:\inetpub\wwwroot\SalesSpecialist\pre-sales-technical",
                Product = @"C:\Domains\MicrosoftSalesSpecialist.com\pre-sales-technical"
            });
            config.FolderPaths.Add("Sales-Specialist".ToLower(), new FolderPath()
            {
                Staging = @"C:\inetpub\wwwroot\SalesSpecialist\sales-specialist",
                Product = @"C:\Domains\MicrosoftSalesSpecialist.com\sales-specialist"
            });
            config.FolderPaths.Add("MSSalesSpecialist".ToLower(), new FolderPath()
            {
                Staging = @"C:\inetpub\wwwroot\MSSalesSpecialist",
                Product = @"C:\Domains\mssalesspecialist.com\wwwroot"
            });

            /*Dictionary<string, DataItem> dic = new Dictionary<string, DataItem>();
            dic.Add("1", new DataItem() {Id = 1, Desc = "Desc 1"});
            dic.Add("2", new DataItem() {Id = 2, Desc = "Desc 2"});
            dic.Add("3", new DataItem() {Id = 3, Desc = "Desc 3"});*/
            using (StreamWriter sw = new StreamWriter("TestDictionary.json"))
            {
                sw.WriteLine(JsonConvert.SerializeObject(config));
            }
            FileInfo fi = new FileInfo("TestDictionary.json");
        }

        [Test]
        public void Extract()
        {
            string file = "TestZip\\2013-03-29 13-35.zip";
            ZipEntry[] arr;
            int i = 0;
            string tmp;
            using (ZipFile zip = ZipFile.Read(file))
            {
                arr = zip.Entries.ToArray();
                foreach (var ze in arr)
                {
                    tmp = ze.FileName;
                    ze.FileName = ze.FileName.Substring(ze.FileName.IndexOf("/") + 1);
                    ze.Extract("TestZip\\", ExtractExistingFileAction.OverwriteSilently);
                    ze.FileName = tmp;
                    i++;
                }
            }
            int a;
            a = 5;
        }

        public class DataItem
        {
            public int Id { get; set; }
            public string Desc { get; set; }
        }
    }
}
